<?php

namespace App\Http\Controllers;

use App\Product;
use App\ProductClass;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Admin;

class AdminController extends Controller
{
    /*
     * login()
     * 渲染登录页面
     * */
    public function login(){
        return view('admin.login');
    }


    /**
     * sign渲染注册页面
     */
    public function sign(){
        return view('admin.sign');
    }


    /*
     * signDo注册数据校验
     * */
    public function signDo(Request $request){;
        $data = Admin::create([
            'username'=>$request->username,
            'password'=>$request->password,
        ]);
        if($data){
//            $result['code']='200';
//            $result['message']='注册成功';
            return view('admin.login')->with(['message'=>'注册成功','url'=>url("views/admin/login")]);
        }else{
            $result['code']='400';
            $result['message']='注册失败';
            $result['data']=null;
        }
    }


    /*
     * loginDo登陆数据校验
     * $username 用户名
     * $password 密码
     * */
    public function loginDo(Request $request){
        $token =  md5($request->username.$request->password.time());
        $data = Admin::where(['username'=>$request->username,'password'=>$request->password])->first();
//        var_dump($data);die();
        if($data == ''){
            return back();
        }
        $data->update([
            'token'=>$token,
        ]);
        if($data){
            $result['code']='200';
            $result['message']='登陆成功';
            $result['data']['token']=$token;
            return $result;
        }else{
            $result['code']='400';
            $result['message']='登陆失败';
            $result['data']=null;
            return $result;
        }
    }
    /*
     * first()
     * 渲染主页面
     * */
    public function first(){
        return view('admin.first');
    }
    /*
     * welcome()
     * 渲染welcome页面
     * */
    public function welcome(){
        return view('admin.welcome');
    }
    /*
     * cate()
     * 商品分类展示
     * $data  数据库中分类数据
     * */
    public function cate(){
        $data = ProductClass::get();
        return view('admin.cate',compact('data'));
    }
    public function admin_edit($id){
        $data = ProductClass::findOrFail($id);
        return view('admin.admin_edit',compact('data'));
    }
    public function admin_editDo(Request $request,$id){
        $data = ProductClass::findOrFail($id);
        $data->update([
            'name'=>$request->name,
            'parent_id'=>$request->parent_id,
        ]);
     return back();
    }
    public function add(Request $request){
        $data = ProductClass::create([
            'name'=>$request->name,
            'parent_id'=>$request->parent_id,
        ]);
        //分类名添加页面
        if($data){
            return back();
        }
    }
    public function project_Administrator(){
        $data = Product::get();
        return view('admin.project',compact('data'));
    }
    public function save($id){
//        return $id;die;
        //修改商品信息
        $data = Product::findOrFail($id);
        return view('admin.update',compact('data'));
    }
    public function do_upt(Request $request,$id){
        return 1;die;
            //对数据库进行修改操作
            $data = ProductClass::findOrFail($id);
            $data->update([
                'name'=>$request->name,
                'parent_id'=>$request->parent_id,
            ]);
            return back();
        }

    /*
 * insert()
 * 商品添加页面
 * */
    public function insert(){
        //添加商品页面
        return view('admin.insert');
    }
    public function do_ist(Request $request){
        //添加到数据库
//        var_dump($request->title);
       $res = Product::create([
            'title'=>$request->title,
            'img'=>$request->img,
            'catagory_id'=>$request->catagory_id,
            'price'=>$request->price,
            'inventory'=>$request->inventory,
        ]);
       if($res){
           return back();
       }else{
           echo '添加失败';
       }
    }
    public function del($id){
        //商品分类删除
//        var_dump($id);die;
        $data = ProductClass::findOrFail($id);
        $data -> delete();
        return back();
    }
    public function proDel($id){
        //商品删除
        $data = Product::FindOrFail($id);
        $res = $data->delete();
        if($res){
            return back();
        }else {
            echo '删除失败';
        }
    }
}
